package com.icezzz.screen;

import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ScreenController {

	// http://127.0.0.1:8080/screen
	@RequestMapping(value = "screen", produces = MediaType.IMAGE_PNG_VALUE)
	public byte[] screen() {
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Rectangle screenRect = new Rectangle(screenSize);
		BufferedImage bufferedImage = null;
		try {
			bufferedImage = new Robot().createScreenCapture(screenRect);
		} catch (AWTException e) {
			e.printStackTrace();
		}
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		try {
			ImageIO.write(bufferedImage, "png", outputStream);
			byte[] byteArray = outputStream.toByteArray();
			return byteArray;
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		return new byte[1];
	}
}
